package com.kyzh.core.pager.weal.address

import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Address
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.utils.aesEcb
import com.kyzh.core.utils.startActivity
import kotlinx.android.synthetic.main.act_address.*
import org.jetbrains.anko.toast


class AddressActivity : BaseActivity(), ResultListener {
    private val beans = mutableListOf<Address>()
    private lateinit var adapter: AddressAdapter
    private var request = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_address)
        initUi()
    }

    private fun initUi() {
        (add.background as GradientDrawable).cornerRadius = 222f
        recyclerview.layoutManager = LinearLayoutManager(this@AddressActivity)
        add.setOnClickListener { startActivity<AddressNewActivity>(GlobalKeys.UID to MMKVConsts.uid) }
        adapter = AddressAdapter(R.layout.act_address_item, beans)
        recyclerview.adapter = adapter
        root.setEnableLoadMore(false)
        root.setOnRefreshListener { UserImpl.selectAddress(this) }
        request = intent.getBooleanExtra(GlobalKeys.SELECT,false)
        titleView.setText(getString(if (request) R.string.selectAddress else R.string.addressManage))
        adapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.delete -> {
                    UserImpl.deleteAddress(beans[position].id, object : ResultListener {
                        override fun success(message: Any) {
                            toast(message as String)
                            beans.removeAt(position)
                            adapter.notifyDataSetChanged()
                        }

                        override fun error(error: String) {
                            toast(error)
                        }
                    })
                }
                R.id.set_default -> {
                    val address = beans[position]
                        val map = HashMap<String,String>()
                        map["consignee"] = address.consignee
                        map["mobile"] = address.mobile
                        map["uaddress"] = address.uaddress
                        map["moren"] = "1"
                        val value = aesEcb(Gson().toJson(map))
                        UserRequest.changeAddress(value,address.id){
                            for (it in beans) {
                                if (it.id ==address.id){
                                    it.moren = "1"
                                }else{
                                    it.moren = "0"
                                }
                            }
                            adapter.notifyDataSetChanged()
                        }

                }
                R.id.content->{
                    if (request){
                        val result = Intent()
                        result.putExtra(GlobalKeys.SELECT, beans[position])
                        setResult(RESULT_OK, result)
                        finish()
                    }else{
                        startActivity<AddressEditActivity>(GlobalKeys.ADDRESS to beans[position])
                    }
                }
                 R.id.edit -> {
                    startActivity<AddressEditActivity>(GlobalKeys.ADDRESS to beans[position])
                }
            }
        }
    }


    override fun onResume() {
        super.onResume()
        root.autoRefresh()
    }

    override fun success(bean: Any) {
        beans.clear()
        beans.addAll(bean as ArrayList<Address>)
        adapter.notifyDataSetChanged()
        root.finishRefresh()
    }

    override fun error(error: String) {
        toast(error)
        root.finishRefresh()
        adapter.notifyDataSetChanged()
    }


    inner class AddressAdapter(layout: Int, val beans: MutableList<Address>) : BaseQuickAdapter<Address, BaseViewHolder>(layout, beans) {
        init {
            addChildClickViewIds(R.id.edit,R.id.content,R.id.delete,R.id.set_default)
        }
        override fun convert(helper: BaseViewHolder, item: Address) {
            item.let {
                helper.setText(R.id.name, it.consignee)
                        .setText(R.id.phone, it.mobile)
                        .setText(R.id.uaddress, it.uaddress)
                        .setVisible(R.id.moren, it.moren == "1")
            }
        }
    }

}
